package com.netty.client.pool.active;

import io.netty.channel.ChannelHandlerContext;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.netty.client.QbbClient;
import com.qbb.client.exception.HandlerException;
import com.qbb.client.security.SecurityUtil;
import com.qbb.client.util.DateUtil;

/**
 * @author 作者 : 陈怀傲
 * @version 创建时间：2015年4月14日 下午2:51:45 类说明
 */
public class ThreadActiveCall {
	private static Log log = LogFactory.getLog(ThreadActiveCall.class);

	private ThreadActiveBean channelBean;

	private ThreadActiveCall() {
	}

	public ThreadActiveCall(ThreadActiveBean channelBean) {
		this.channelBean = channelBean;
	}

	public void execute() {
		ChannelHandlerContext ctx = channelBean.getCtx();

		String descurityStr = null;
		try {
			String uuid = null;
			try {
				synchronized (QbbClient.activeQueue) {
					// get uuid and remove uuid
					uuid = QbbClient.activeQueue.poll();
				}

			} catch (Exception e) {
				HandlerException.error(e);
			}

			try {
				String fnString = QbbClient.activeMaps.get(uuid);
				QbbClient.activeMaps.remove(uuid);

				String resourceStr = uuid + "|" + fnString;

				String finalStr = SecurityUtil.getFinalStr(resourceStr);
				descurityStr = SecurityUtil.getSecurityStr(finalStr,
						SecurityUtil.kek, 0);
			} catch (Exception e) {
				HandlerException.error(e);
			}
		} catch (Exception e) {
			HandlerException.error(e);
		} finally {
			if (descurityStr == null) {
				descurityStr = "";
			}
			ctx.write(descurityStr);
			ctx.flush();

			Date endDate = new Date();
			String endStr = DateUtil.date_YYYY_MM_DD_MM_HH_SS(endDate);
			log.info("QbbClient channelActive end[" + endStr + "]");
		}
	}

}
